//用户账号校验
function checkAccount(ID){
    return new RegExp('^[a-zA-Z0-9_-]{4,16}$').test(ID)
}
//验证码刷新
function getKaptchar(){

    let code = $.ajax({
        url:$('#kaptcharApi').attr('action'),
        method: 'get',
        dataFilter: function(data,type){
            let dataBody = JSON.parse(data)
            console.log("codeId:"+dataBody.codeId)
            let ImgBuffered = dataBody.data
            loginVue.$data.codeId = dataBody.codeId
            $('#kaptcharImg').attr('src',"data:image/png;base64,"+ImgBuffered)
        }
    })
    // loginVue.$data.codeId = code
    // alert(JSON.stringify(code))
}

//复选框触发事件
let form = layui.form
form.on('checkbox(choice)',function (data){
    if(data.elem.checked){
        loginVue.$data.choice = 1
    }
})


let loginVue = new Vue({
    el:"#login_form",
    data:{
        account:'',
        password:'',
        code:'',
        codeId:'',
        choice:0

    },
    methods:{
        login_reset:function (event) {
            event.preventDefault()
            this.account = ''
            this.password = ''
            this.code = ''
            this.codeId = ''
            this.choice = 0
        },

        login_submit:function (event) {
            event.preventDefault()
            if(this.account !== '' &&
                this.password !=='' &&
                this.code !==''

            ){
                if(checkAccount(this.account)){
                    axios({
                        url:$('#login_form').attr('action'),
                        method:'post',

                        params: {
                            name: this.account,
                            pwd: this.password,
                            code: this.code,
                            codeId: this.codeId,
                            choice: this.choice

                        }
                    }).then(function (response){
                        // console.log(JSON.stringify(response.data))
                        // alert(JSON.stringify(response.data))
                        if(response.data.code === 200){
                            setTimeout(function (){
                                history.go(0)
                            },800)
                        }else {
                            layer.msg(response.data.msg)
                            if(response.data.code === 404 || response.data.code === 400 ){
                                getKaptchar()
                                loginVue.$data.password = ''
                                loginVue.$data.code = ''
                                loginVue.$data.choice = 0
                                loginVue.$data.codeId = ''
                                $('#password').val('')
                                $('#code').val(null)
                            }

                        }





                    })

                }else {
                    layer.msg("无效用户名",{
                        time:2500
                    })
                }
            }

        }
    }




})

$(document).ready(function (){
    //进入页面自动生成验证码
    getKaptchar()

    //换验证码
    $('#kaptcharImg').click(function (){
        getKaptchar()
    })

})